package softtech.apps.callblocker.adapter;

import java.util.ArrayList;
import java.util.zip.Inflater;

import softtech.apps.utility.MyDialog;
import softtech.apps.callblocker.R;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.HeaderPhone;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class AdapterSmsSettingHeader extends BaseAdapter {

	MyDialog myDialog;
	LayoutInflater inf;
	ArrayList<HeaderPhone> mListHeaderPhone;
	Context context;

	public AdapterSmsSettingHeader(ArrayList<HeaderPhone> item,
			LayoutInflater inf, Context context) {
		this.mListHeaderPhone = item;
		this.inf = inf;
		this.context = context;
	}

	int count = 30;

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mListHeaderPhone.size() >= count) {
			return count;
		}
		if(mListHeaderPhone.size()<=0){
			return 1;
		}
		return mListHeaderPhone.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View vi, ViewGroup arg2) {
		
		if (mListHeaderPhone == null || mListHeaderPhone.equals(null)
				|| mListHeaderPhone.size() == 0) {
			vi = inf.inflate(R.layout.item_search_notfound,
					null);
			return vi;
		}
		ViewHolder viewholder = new ViewHolder();
		if(vi==null || vi.getTag() == null){
			vi = inf.inflate(R.layout.page_setting_sms_item_headerphone, null);
			viewholder.btSmsHeaderDelete = (Button) vi
					.findViewById(R.id.btsmssettingHeaderDelete);
			viewholder.tvSmsHeaderView = (TextView) vi
					.findViewById(R.id.tvsmsSettingheader);
			vi.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) vi.getTag();
		}
		final int id = position;
		viewholder.tvSmsHeaderView.setText(mListHeaderPhone.get(position)
				.getHeader());
		viewholder.btSmsHeaderDelete
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						
						AlertDialog.Builder builder = new AlertDialog.Builder(context);
						DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								switch (which) {
								case DialogInterface.BUTTON_POSITIVE:
									// No button clicked

									break;

								case DialogInterface.BUTTON_NEGATIVE:
									// Yes button clicked
									DatabaseHanler db = new DatabaseHanler(context);
									db.deleteHeaderPhoneSms(HeaderPhone.HEADER + " =?",
											new String[] { mListHeaderPhone.get(id)
													.getHeader() });
									mListHeaderPhone.remove(id);
									update(mListHeaderPhone);
									
									
									break;
								}
							}
						};

						builder.setMessage("Are you sure?")
								.setNegativeButton("Yes", dialogClickListener)
								.setPositiveButton("No", dialogClickListener).show();
					}
				});
		final ViewHolder view = viewholder;
		viewholder.tvSmsHeaderView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				OnClickListener leftClick = new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (myDialog.getKeyCodeString().length() == 0) {
							return;
						}
						view.tvSmsHeaderView.setText(""+myDialog
								.getKeyCodeString().trim());
					}
				};
				myDialog = new MyDialog(context, "Edit new Header",
						"Edit new Header", "Save", "Cancel", leftClick,
						null, view.tvSmsHeaderView.getText().toString().trim());
				myDialog.showDialog();
			}
		});
		return vi;
	}

	public void update(ArrayList<HeaderPhone> item) {
		mListHeaderPhone = item;
		notifyDataSetChanged();
	}

	static class ViewHolder {
		/**
		 * delete Header phone
		 */
		Button btSmsHeaderDelete;
		/**
		 * View header phone
		 */
		TextView tvSmsHeaderView;
	}

}
